package io.github.kouleen.manager;

import io.github.kouleen.manager.service.ManagerXConnectService;
import io.github.kouleen.minecraft.core.lang.annotation.MinecraftPluginMain;
import io.github.kouleen.minecraft.plugin.MinecraftPluginRun;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhangqing
 * @since 2025/1/25 14:15
 */
@MinecraftPluginMain
public final class ManagerX extends JavaPlugin {

    // Map<插件名称插件版本,插件实例>
    private final Map<String, Plugin> pluginMap = new HashMap<>();

    @Override
    public void onLoad() {
        MinecraftPluginRun.start(this, getClassLoader());
    }

    @Override
    public void onEnable() {
        ManagerXConnectService.initConfig(this);
        ManagerXConnectService.runTaskTimerAsynchronously(this);
    }

    public Plugin getPlugin(String pluginKey) {
        return this.pluginMap.get(pluginKey);
    }

    public void setPlugin(String pluginKey,Plugin plugin) {
        Plugin pluginInstance = getPlugin(pluginKey);
        if(Objects.isNull(pluginInstance)){
            this.pluginMap.put(pluginKey,plugin);
            return;
        }
        throw new IllegalArgumentException(plugin.getName() + "已存在");
    }

    public void removePlugin(String pluginKey) {
        Plugin pluginInstance = getPlugin(pluginKey);
        if(Objects.nonNull(pluginInstance)){
            this.pluginMap.remove(pluginKey);
        }
    }
}
